//
// Created by 崔世强 on 2018/4/2.
// Copyright (c) 2018 Facebook. All rights reserved.
//

import Foundation
import SwiftyJSON

class NoticeHandlerFactory {
    static let instance = NoticeHandlerFactory()

    private init() {
    }

    func getHandler(_ msg: Message) -> NoticeHandlerProtocol {
        let json = JSON(parseJSON: msg.content ?? "")

        if let type = NoticeType(rawValue: json["type"].int ?? -1) {
            switch type {
            case .modifyGroupName:
                return ModifyGroupNameHandler.instance
            case .modifyGroupNickName:
                return ModifyGroupNickNameHandler.instance
            case .modifyGroupAnnouncement:
                return ModifyGroupAnnouncementHandler.instance
            case .muteAll:
                return MuteAllHandler.instance
            case .mute:
                return MuteHandler.instance
            case .unMuteAll:
                return UnMuteAllHandler.instance
            case .unMute:
                return UnMuteHandler.instance
            case .addMember:
                return AddMemberHandler.instance
            case .delMember:
                return DelMemberHandler.instance
            case .changeMemberRole:
                return ChangeMemberRoleHandler.instance
            case .memberQuit:
                return MemberQuitHandler.instance
            case .changeMaster:
                return ChangeMasterHandler.instance
            default:
                return DefaultNoticeHandler.instance
            }
        } else {
            return DefaultNoticeHandler.instance
        }
    }

    func getHandler(_ msg:ChatMessage) -> NoticeHandlerProtocol {
        let json = JSON(parseJSON: msg.text ?? "" )

        if let type = NoticeType(rawValue: json["type"].int ?? -1) {
            switch type {
            case .modifyGroupName:
                return ModifyGroupNameHandler.instance
            case .modifyGroupNickName:
                return ModifyGroupNickNameHandler.instance
            case .modifyGroupAnnouncement:
                return ModifyGroupAnnouncementHandler.instance
            case .muteAll:
                return MuteAllHandler.instance
            case .mute:
                return MuteHandler.instance
            case .unMuteAll:
                return UnMuteAllHandler.instance
            case .unMute:
                return UnMuteHandler.instance
            case .addMember:
                return AddMemberHandler.instance
            case .delMember:
                return DelMemberHandler.instance
            case .changeMemberRole:
                return ChangeMemberRoleHandler.instance
            case .memberQuit:
                return MemberQuitHandler.instance
            case .changeMaster:
                return ChangeMasterHandler.instance
            default:
                return DefaultNoticeHandler.instance
            }
        } else {
            return DefaultNoticeHandler.instance
        }
    }
}
